#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
int h[N], e[M], ne[M], idx;
int out[N], in[N];
int d[N];
int n, m;
int ans;
void add(int a, int b)
{
	e[idx] = b; 
	ne[idx] = h[a];
	h[a] = idx++;
}
void tuopu()
{
	queue<int>q;
	for (int i = 1; i <= n; i++) {
		if (!in[i]) {
			d[i] = 1;
			q.push(i);
		}
	}
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			d[j] += d[t];
			if (--in[j] == 0) {
				if (out[j]) {
					q.push(j);
				}
				else {
					ans += d[j];
				}
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		in[b]++;
		out[a]++;
	}
	tuopu();
	cout << ans << endl;
	return 0;
}